package me.monster.smarttraffic.ui.road;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatSpinner;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import me.monster.smarttraffic.R;
import me.monster.smarttraffic.base.AbstractPresenterActivity;
import me.monster.smarttraffic.bo.RoadLight;

/**
 * @description
 * @author: Created jiangjiwei in 2020-02-14 10:19
 */
public class RoadActivity extends AbstractPresenterActivity<RoadPresenter> implements RoadContract.View {

    @BindView(R.id.sp_road)
    AppCompatSpinner spRoad;
    @BindView(R.id.btn_road_query)
    Button btnQuery;
    @BindView(R.id.rv_road_light)
    RecyclerView rvLight;
    @BindView(R.id.pb_road_light)
    ProgressBar pbLoading;
    private List<RoadLight> roadLightList = new ArrayList<>();
    private LightAdapter adapter;

    public static void start(Activity context) {
        Intent starter = new Intent(context, RoadActivity.class);
        context.startActivity(starter);
        context.overridePendingTransition(R.anim.translate_in, R.anim.alpha_out);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter.fetchLightRules();
        adapter = new LightAdapter(roadLightList);
        rvLight.setAdapter(adapter);
        spRoad.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                mPresenter.compareLights(i);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }

    @Override
    public RoadPresenter createPresenter() {
        return new RoadPresenter();
    }

    @Override
    public View closeToolView() {
        return findViewById(R.id.tv_road_tittle);
    }

    @Override
    public String getInitTitle() {
        return getString(R.string.dash_menu_r);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_road;
    }

    @Override
    public void showLights(List<RoadLight> lightList) {
        roadLightList.clear();
        roadLightList.addAll(lightList);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void bindSpinner(List<Map<String, String>> spinnerList) {
        SimpleAdapter simpleAdapter = new SimpleAdapter(this, spinnerList, android.R.layout.simple_list_item_1, new String[]{"name"}, new int[]{android.R.id.text1});
        spRoad.setAdapter(simpleAdapter);
    }

    @Override
    public void showLoading() {
        pbLoading.setVisibility(View.VISIBLE);
    }

    @Override
    public void dismissLoading() {
        pbLoading.setVisibility(View.GONE);
    }

    @Override
    public void showError(int error) {
        switch (error) {
            case RoadPresenter.ERROR_LIST_RULE:
                toast("获取排序规则出错");
                break;
            case RoadPresenter.ERROR_LIST_LIGHT:
                toast("获取信号灯异常");
                break;
            default:
        }
    }

    @OnClick(R.id.btn_road_query)
    public void btnQuery() {
        mPresenter.fetchLight();
    }
}
